package com.beone.smart.smartBeauty.task;

import com.beone.smart.smartBeauty.dao.*;
import com.beone.smart.smartBeauty.model.entity.*;
import com.beone.smart.smartBeauty.utils.LocalTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * Created by bruce on 20/2/25 -- 14:45
 * 定时任务往数据库添加数据
 */
@Slf4j
@EnableScheduling
@Configuration
public class TimerTask {
    @Autowired
    private SkinGatherDao skinGatherDao;
    @Autowired
    private BeautyDeviceDao beautyDeviceDao;
    @Autowired
    private SkinRankDao skinRankDao;
    @Autowired
    private ProductGatherDao productGatherDao;
    @Autowired
    private WeightRecordDao weightRecordDao;
    @Autowired
    private SleepInfoRecordDao sleepInfoRecordDao;
    @Autowired
    private DeviceInfoDao deviceInfoDao;

    //@Scheduled(cron="seconds minutes hours day month week")
    //@Scheduled(cron="seconds minutes hours day month week year")
    @Scheduled(fixedRate = 10 * 60 * 60 * 1000)
//    @Scheduled(fixedRate = 5 * 1000)
    public void insertData() {
        insertSkinGather();
        insertSleepInfoData();
        insertBeautyDevice();
        insertSkinRank();
        insertProductGather();
        insertWeightRecord();
    }

    /**
     * 插入睡眠数据
     */
    private void insertSleepInfoData() {
        DeviceInfoDO deviceInfoDO = getDeviceInfo();
        if (deviceInfoDO != null) {
            SleepInfoRecordDO sleepInfoRecordDO = new SleepInfoRecordDO();
            //
            sleepInfoRecordDO.setUserId(deviceInfoDO.getUserId());
            sleepInfoRecordDO.setDev_code(deviceInfoDO.getDev_code());
            sleepInfoRecordDO.setDev_name(deviceInfoDO.getDev_name());
            sleepInfoRecordDO.setUserName(deviceInfoDO.getUserName());
            //
            sleepInfoRecordDO.setScore(createScore());
            sleepInfoRecordDO.setDeepHour(createDeep(1, 4));
            sleepInfoRecordDO.setMiddleHour(createDeep(2, 5));
            sleepInfoRecordDO.setShallowHour(createDeep(2, 3));
            sleepInfoRecordDO.setRecordDate(LocalTimeUtil.getDateTime());
            sleepInfoRecordDao.insert(sleepInfoRecordDO);
        }
    }

    private DeviceInfoDO getDeviceInfo() {
        List<DeviceInfoDO> deviceInfoDOList = deviceInfoDao.selectList(null);
        if (deviceInfoDOList != null && deviceInfoDOList.size() > 0) {
            Collections.shuffle(deviceInfoDOList);
            return deviceInfoDOList.get(0);
        }
        return null;
    }

    private double createDeep(int level, int top) {
        Random random = new Random();
        double value = random.nextDouble();
        double result = value * 10;
        if (result > top) {
            return Math.abs(top - result);
        }
        if (result < level) {
            return Math.abs(result);
        }
        return level;
    }

    private int createScore() {
        Random random = new Random();
        double value = random.nextDouble();
        return (int) (80 + value * 20);
    }

    private Double createBmi() {
        Random random = new Random();
        double value = random.nextDouble();
        double result = 20 + value * 10;
        if (result > 30) {
            return 30 - value * 10;
        } else {
            return result;
        }
    }

    private Double createWeight() {
        Random random = new Random();
        double value = random.nextDouble();
        double result = 60 + value * 50;
        if (result > 100) {
            return 100 - value * 10;
        } else {
            return result;
        }
    }

    /**
     * 插入体重数据
     */
    private void insertWeightRecord() {
        DeviceInfoDO deviceInfoDO = getDeviceInfo();
        if (deviceInfoDO != null) {
            WeightRecordDo weightRecordDo = new WeightRecordDo();
            weightRecordDo.setBmiNumber(createBmi());
            weightRecordDo.setRecordDate(LocalTimeUtil.getDateTime());
            weightRecordDo.setWeightRecord(createWeight());
            //
            weightRecordDo.setUserName(deviceInfoDO.getUserName());
            weightRecordDo.setUserId(deviceInfoDO.getUserId());
            weightRecordDo.setDev_code(deviceInfoDO.getDev_code());
            weightRecordDo.setDev_name(deviceInfoDO.getDev_name());
            weightRecordDao.insert(weightRecordDo);
        }
    }

    /**
     * 插入产品统计数据
     */
    private void insertProductGather() {
        productGatherDao.insert(createProduct());
    }

    private static ProductGatherDO createProduct() {
        ProductGatherDO[] list = {
                new ProductGatherDO("1008612", "补水面膜"),
                new ProductGatherDO("1008613", "爽肤水"),
                new ProductGatherDO("1008614", "精华液"),
                new ProductGatherDO("1008615", "大宝"),
                new ProductGatherDO("1008616", "面膜")
        };
        int index = (int) Math.floor(Math.random() * list.length);
        return list[index];
    }

    /**
     * 插入肤质排名数据
     */
    private void insertSkinRank() {
        SkinRankDO skinRankDO = new SkinRankDO();
        skinRankDO.setArea(createArea());
        skinRankDO.setDateTime(LocalTimeUtil.getDateTime());
        skinRankDO.setLevel(parseDouble());
        skinRankDao.insert(skinRankDO);
    }

    /**
     * 插入肤质统计数据
     */
    private void insertSkinGather() {
        DeviceInfoDO deviceInfoDO = getDeviceInfo();
        if (deviceInfoDO != null) {
            SkinGatherDO skinGatherDO = new SkinGatherDO();
            skinGatherDO.setDoudou(parseDouble());
            skinGatherDO.setFuse(parseDouble());
            skinGatherDO.setMaokong(parseDouble());
            skinGatherDO.setSeban(parseDouble());
            skinGatherDO.setTanxin(parseDouble());
            skinGatherDO.setXiwen(parseDouble());
            skinGatherDO.setRecordDate(LocalTimeUtil.getDateTime());
            //水分、油分
            skinGatherDO.setWaterValue(10 + parseDouble());
            skinGatherDO.setOilValue(10 + parseDouble());
            //
            skinGatherDO.setUserName(deviceInfoDO.getUserName());
            skinGatherDO.setUserId(deviceInfoDO.getUserId());
            skinGatherDO.setDev_code(deviceInfoDO.getDev_code());
            skinGatherDO.setDev_name(deviceInfoDO.getDev_name());
            skinGatherDao.insert(skinGatherDO);
        }
    }

    /**
     * 插入美容设备数据
     */
    private void insertBeautyDevice() {
        String title = createTitle();
        BeautyDeviceDO beautyDeviceDO = new BeautyDeviceDO();
        beautyDeviceDO.setImgUrl("testurl");
        beautyDeviceDO.setTitle(title);
        beautyDeviceDO.setSubTitle(title);
        beautyDeviceDO.setArea(createArea());
        beautyDeviceDO.setJoinDate(LocalTimeUtil.getDateTime());
        beautyDeviceDao.insert(beautyDeviceDO);
    }

    private String createTitle() {
        Random random = new Random();
        String[] areas = {"智能脸部按摩仪", "按摩椅", "睡眠手环", "体脂秤", "跑步机", "除螨仪"};
        return areas[random.nextInt(6)];
    }

    private static String createArea() {
        Random random = new Random();
        String[] areas = {"四川", "重庆", "北京", "上海", "广州", "深圳"};
        return areas[random.nextInt(6)];
    }

    public static void main(String[] args) {
        System.out.println(createProduct());
    }

    private Double parseDouble() {
        Random random = new Random();
        double value = random.nextDouble();
        double result = 4.5 + value * 10;
        if (result > 10) {
            return value * 10;
        } else {
            return result;
        }
    }
}
